const ip_port = "http://47.96.4.230:11043"

function fillHeader() {
    navigator.getBattery().then(function (battery) {
        // 是否正在充电，yes-充电
        console.log("Battery charging? " + (battery.charging ? "yes" : "no"));
        // 当前剩余电量
        console.log("Battery level: " + battery.level * 100 + "%");
        // 填充电量信息
        fillText("energy-value-text", parseInt(battery.level * 100) + "%");
        // alert("设备电量:" + battery.level * 100 + "%");

        // 监听充电状态改变
        battery.addEventListener("chargingchange", function () {
            console.log("Battery charging? " + (battery.charging ? "yes" : "no"));
        });

        // 监听剩余电量改变
        battery.addEventListener("levelchange", function () {
            console.log("Battery level: " + battery.level * 100 + "%");
            fillText("energy-value-text", battery.level * 100 + "%");
        });
    });

    // 60s更新一次天气信息
    fillValue();
    setInterval(fillValue, 60000);

    function fillValue() {
        // 使用客户端公网ip获取所在省市天气
        promise = ajax(opts = {url: ip_port + "/getWeather/", data: {"cip": cip}})
        // promise = ajax(opts = {url: "http://47.96.4.230:11043/testWeather"})
        promise.then(function (successMessage) {
            console.log(successMessage);
            fillText("air-quality-text", successMessage.level);
            fillText("pm25-text", successMessage.ps_pm25);
            fillText("weather-text", successMessage.weather);
            fillText("temperature-upper-text", successMessage.temperature_day + "℃")
            fillText("temperature-current-text", successMessage.temperature + "℃");
            fillText("temperature-lower-text", successMessage.temperature_night + "℃");
        })
    }
}

function fillBody() {
    function updateTime() {
        date = new TimeGeneration();
        setInterval(function () {
            render(date.getDate(),date.getCalendarDate());
        }, 1000);
    }

    // 渲染事件日期
    function render(date, calendarDate) {
        // 时间，时间不使用TimeGeneration的，可能造成跳秒，直接在此处获取Date()
        let time = new Date();
        // setNumImage("time-hh", parseInt(time.getHours() / 10), "red");
        setNumImage("time-hh", parseInt(time.getHours() / 10),);
        // setNumImage("time-hl", parseInt(time.getHours() % 10), "red");
        setNumImage("time-hl", parseInt(time.getHours() % 10), );
        // setNumImage("time-mh", parseInt(time.getMinutes() / 10), "red");
        setNumImage("time-mh", parseInt(time.getMinutes() / 10), );
        // setNumImage("time-ml", parseInt(time.getMinutes() % 10), "red");
        setNumImage("time-ml", parseInt(time.getMinutes() % 10), );
        // setNumImage("time-sh", parseInt(time.getSeconds() / 10), "red");
        setNumImage("time-sh", parseInt(time.getSeconds() / 10),);
        // setNumImage("time-sl", parseInt(time.getSeconds() % 10), "red");
        setNumImage("time-sl", parseInt(time.getSeconds() % 10), );

        // 日期

        // 农历
    }

    updateTime();
}

window.onload = function dataUpdate() {
    fillHeader();

    fillBody();
}
